package org.lw.consumer;

import com.alibaba.cloud.nacos.loadbalancer.NacosLoadBalancerClientConfiguration;
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@SpringBootApplication
@EnableDiscoveryClient   //开启服务注册与发现
@EnableFeignClients      //开启feign
@LoadBalancerClients(value = {@LoadBalancerClient(value = "provider-server", configuration = NacosLoadBalancerClientConfiguration.class)})

@EnableWebMvc
public class NacosConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerApplication.class,args);

    }
    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
    }
   /* @Bean
    @LoadBalanced //开启负载均衡
    public RestTemplate template(){
        return new RestTemplate();
    }*/
}

/*
*   feign 的使用
*    1 、导入依赖包  openfeign
*         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
*   2 、编写接口 (feign)
        @Component
        @FeignClient(value = "NacosProvider",fallbackFactory = ProviderClientFallBack.class)  // 服务名称
        public interface ProviderClient {
             @PostMapping("/provider")  //路径
               public String test(@RequestParam("username") String username);
        }
*       //当请求ProviderClient的test方法时，会调用NacosProvider服务的/provider路径，返回结果。(******)
        //发送的数据可以以参数的形式传递。(********)
      大致的原理： 
          openfeign会通过动态代理的方式，创建接口对象，并将对象村放到容器里。  
*   3 、开启feign
*       再启动类添加  @EnableFeignClients
*       删除restTemplate
    4、服务的调用发可以直接使用  ProviderClient对象的test方法，进行远程调用。

       public class UserClientController {

           @Autowired
           ProviderClient providerClient;  //他可以调用test
           @PostMapping ("/{username}")
           public String getInfoByUsername(@PathVariable String username){
                     return providerClient.test(username);
           }
}
*   
*
*
* */
